﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using final_darskade.Models;

namespace final_darskade.Controllers
{
    public class contactManagementController : Controller
    {
        private darskadeViewsEntities viewsEntities = new darskadeViewsEntities();
        private darskadeEntities tablesEntities = new darskadeEntities();

        public ActionResult Index()
        {
            if (Session["UserName"] == null)
                return RedirectToAction("Index", "Account");
            return View("contactList", tablesEntities.users.ToList());
        }


        //
        // GET: /contactManagement/Details/5

        public ActionResult Details(int user_id)
        {
            DetailOfUser detail = new DetailOfUser();
            detail.fillProperties(user_id);
            return View(detail);
        }

        //
        // GET: /contactManagement/Create
        [HttpGet]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /contactManagement/Create

        [HttpPost]

        //public ActionResult Create([Bind(Exclude="security_question,security_answare,is_first_login")]user creatContact)
        public ActionResult Create(string username, string pass, Nullable<int> role_id, string email, string first_name, string last_name, Nullable<DateTime> entrance_year)
        {
            //check values
            if (String.IsNullOrEmpty(username))
                ModelState.AddModelError("username", "Username Required");

            if (String.IsNullOrEmpty(pass))
                ModelState.AddModelError("pass", "Password Required");
            if (role_id == null)
                ModelState.AddModelError("role_id", "Role ID Required");
            if (String.IsNullOrEmpty(email))
                ModelState.AddModelError("email", "Email Required");
            if (String.IsNullOrEmpty(first_name))
                ModelState.AddModelError("first_name", "First Name Required");
            if (String.IsNullOrEmpty(last_name))
                ModelState.AddModelError("last_name", "Last Name Required");
            if (entrance_year == null)
                ModelState.AddModelError("entrance_year", "Entrance Year Required");
            //Insert logic here
            user createContact = new user();
            createContact.is_first_login = true;
            createContact.username = username;
            createContact.pass = Helper.getMd5Hash(pass);
            createContact.role_id = (int)role_id;
            createContact.email = email;
            createContact.first_name = first_name;
            createContact.last_name = last_name;
            createContact.entrance_year = entrance_year;
            tablesEntities.AddTousers(createContact);
            tablesEntities.SaveChanges();

            return RedirectToAction("contactList");
        }

        // GET: /contactManagement/Edit/5
        [HttpGet]
        public ActionResult Edit(int user_id)
        {
            user editUser = (from p in tablesEntities.users
                             where p.user_id == user_id
                             select p).FirstOrDefault();
            return View("Edit", editUser);
        }
        //  //post

        [HttpPost]
        public ActionResult Edit(user editUser)
        {

            var OrginalUserToEdit = (from p in tablesEntities.users
                                     where p.user_id == editUser.user_id
                                     select p).FirstOrDefault();

            //don't change password if no password entered.
            if (string.IsNullOrEmpty(editUser.pass)) editUser.pass = OrginalUserToEdit.pass;
            else editUser.pass = Helper.getMd5Hash(editUser.pass);

            tablesEntities.ApplyCurrentValues(OrginalUserToEdit.EntityKey.EntitySetName, editUser);

            tablesEntities.SaveChanges();

            return RedirectToAction("Index");
        }


        // GET: /contactManagement/Delete/5

        public ActionResult Delete(int user_id)
        {
            user deletingUser = (user)(from u in tablesEntities.users
                                        where u.user_id == user_id
                                        select u).Single();
            tablesEntities.users.DeleteObject(deletingUser);

            tablesEntities.SaveChanges();
            return RedirectToAction("Index");
        }
        public ActionResult Search()
        {
            return View("Search");
        }

        [HttpPost]
        public ActionResult Search(int? user_id, string username)
        {
            List<user> model = new List<user>();
            
            user userToFind = null;
            
            if (user_id != null || String.IsNullOrEmpty(username) == false)
            {
                if (user_id == null)
                {
                    userToFind = (user)(from p in tablesEntities.users where p.username.Equals(username) select p).FirstOrDefault();
                }
                else
                {
                    userToFind = (user)(from p in tablesEntities.users where p.user_id == user_id select p).FirstOrDefault();

                }
            }
            if (userToFind != null)
                model.Add(userToFind);

            return View("contactList", model);
        }
        
    }
}
